﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MentorBackEnd.Model;
using System.Data.SqlClient;
using System.Data;

namespace MentorBackEnd.DA
{
    public class CommunicationDA
    {
        public static void SendMessage(int menteeID, int mentorID, string message, int senderID)
        {
            SqlConnection conn = DataAccessor.GetConnection();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ("[or_CommunicationInsert]");
                cmd.Parameters.Add(new SqlParameter("@MenteeID", menteeID));
                cmd.Parameters.Add(new SqlParameter("@MentorID", mentorID));

                cmd.Parameters.Add(new SqlParameter("@Message", message));
                cmd.Parameters.Add(new SqlParameter("@SenderID", senderID));

                cmd.ExecuteNonQuery();
            }

            catch { throw; }
            finally { conn.Close(); }
            return;
        }

        public static List<Communication> GetCommunicationHistory(int menteeID, int mentorID)
        {
            SqlConnection conn = DataAccessor.GetConnection();
            IDataReader reader = null;
            List<Communication> collection = new List<Communication>();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ("[or_MentoringCommunicationSelectByMenteeAndMentor]");
                cmd.Parameters.Add(new SqlParameter("@MenteeID", menteeID));
                cmd.Parameters.Add(new SqlParameter("@MentorID", mentorID));

                cmd.ExecuteNonQuery();

                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Communication dataModel = new Communication(reader);
                    collection.Add(dataModel);
                }

                if (!reader.IsClosed)
                    reader.Close();

                return collection;
            }
            catch { throw; }
            finally { conn.Close(); }
        }
    }
}